<?php

namespace app\service\pixel;

use app\constant\ModelConstant;
use app\model\PixelLogs;
use FacebookAds\Api;
use FacebookAds\Logger\CurlLogger;
use FacebookAds\Object\ServerSide\ActionSource;
use FacebookAds\Object\ServerSide\Content;
use FacebookAds\Object\ServerSide\CustomData;
use FacebookAds\Object\ServerSide\DeliveryCategory;
use FacebookAds\Object\ServerSide\Event;
use FacebookAds\Object\ServerSide\EventRequest;
use FacebookAds\Object\ServerSide\UserData;


class FacebookService
{
    use FacebookTrait;
    const FB_ACCESS_TOKEN = "EAAIhLKhU8NMBOw8hmFlsnlQK8fIBztug3RgilvgdWZBdbSnIKnZAhLRszwzo7BwWOZATnhdYl4fziHZCJZBrVPhzYy1sZASBrtFeWfGBCZCjuxLLSYSaBHqsns8kSTo0qrDZAs6ppTA4sLc0NTK1wJcBcsD4pvQ7ZAbRrGCOAsmltBGYkLvTrZCZAjtFY2lZCA3xUZB5RkgZDZD";
    public function trackViewApi(PixelLogs $log)
    {
        $data = [];
        $pixel_id = $log->pixel_id;
           try {
               Api::init(null, null, self::FB_ACCESS_TOKEN);
               $event = $this->createViewContentEvent($log);
               $data = $event->normalize();
               dump("************Facebook 追踪浏览事件: " . $log->id . ", 像素ID: " . $pixel_id ."**********参数: ", $data);
               $events = array();
               array_push($events, $event);
               $request = (new EventRequest($pixel_id))
                   ->setEvents($events);
               $response = $request->execute();
               $result = [
                   "FbTraceId" => $response->getFbTraceId(),
                   "CustomEndpointResponses" => $response->getCustomEndpointResponses(),
                   "EventsReceived" => $response->getEventsReceived(),
                   "Messages" => $response->getMessages(),
               ];

               dump("************Facebook 追踪浏览事件结果: " . $log->id . ", 像素ID: " . $pixel_id ."**********结果: ", $result);
               $log->trace_id = $response->getFbTraceId();
               $log->response = [
                   "request" => $data,
                   "result" => $result
               ];
               $log->is_track = ModelConstant::STATUS_ON;
               if (!$log->pixel_id) $log->pixel_id = $pixel_id;
               $log->save();
           }catch (\Exception $e){
               dump("************Facebook 追踪浏览事件异常: " . $log->id . ", 像素ID: " . $pixel_id ."**********异常: ", $e->getMessage());
               $log->response = [
                   "request" => $data,
                   "error" => $e->getMessage(),
                   "trace" => $e->getTraceAsString(),
               ];
               $log->is_track = ModelConstant::STATUS_OFF;
               if (!$log->pixel_id) $log->pixel_id = $pixel_id;
               $log->save();
           }

    }

    public function trackPurchaseApi(PixelLogs $log)
    {
        $data = [];
        $pixel_id = $log->pixel_id;
        try {
            //初始化
            Api::init(null, null, self::FB_ACCESS_TOKEN);
            $event = $this->createPurchaseEvent($log);
            $data = $event->normalize();
            dump("************Facebook 追踪购买事件: " . $log->id . ", 像素ID: " . $pixel_id . "**********参数: ", $data);
            $events = array();
            array_push($events, $event);
            $request = (new EventRequest($pixel_id))
                ->setEvents($events);
            $response = $request->execute();
            $result = [
                "FbTraceId" => $response->getFbTraceId(),
                "CustomEndpointResponses" => $response->getCustomEndpointResponses(),
                "EventsReceived" => $response->getEventsReceived(),
                "Messages" => $response->getMessages(),
            ];
            dump("************Facebook 追踪购买事件结果: " . $log->id . ", 像素ID: " . $pixel_id ."**********结果: ", $result);
            $log->trace_id = $response->getFbTraceId();
            $log->response = [
                "request" => $data,
                "result" => $result
            ];
            $log->is_track = ModelConstant::STATUS_ON;
            $log->save();
            return true;
        }catch (\Exception $e){
            dump("************Facebook 追踪购买事件异常: " . $log->id . ", 像素ID: " . $pixel_id ."**********异常: ", $e->getMessage());
            $log->response = [
                "request" => $data,
                "error" => $e->getMessage(),
                "trace" => $e->getTraceAsString(),

            ];
            $log->is_track = ModelConstant::STATUS_OFF;
            $log->save();
        }
    }

}